//
//  DataSummarySleepCell.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit

class DataSummarySleepCell: DataSummaryCell {
    
    @IBOutlet var totalLabel: UILabel!
    @IBOutlet var totalProgressView: UIProgressView!
    @IBOutlet var lightLabel: UILabel!
    @IBOutlet var lightProgressView: UIProgressView!
    @IBOutlet var deepLabel: UILabel!
    @IBOutlet var deepProgressView: UIProgressView!
    
    override func update(vm: DataSummaryCellViewModel, data: Any?) {
        super.update(vm: vm, data: data)
        
        guard let sleepData = (data as? SummaryDataModel)?.sleepMonitorState else {
            showEmptyData()
            return
        }
        
        let deep = sleepData.totalDeepSleepMinute
        let light = sleepData.totalLightSleepMinute
        let total = deep + light
        
        guard total > 0 else {
            showEmptyData()
            return
        }
        
        totalLabel.text = timeDesc(total)
        lightLabel.text = timeDesc(light)
        deepLabel.text = timeDesc(deep)
        totalProgressView.progress = 1
        lightProgressView.progress = Float(light / total)
        deepProgressView.progress = Float(deep / total)
    }
    
    private func timeDesc(_ time: Double) -> String {
        let hours = Int(time / 60)
        let minutes = Int(time) % 60
        return "\(hours)时\(minutes)分"
    }
    
    override func showEmptyData() {
        totalLabel.text = "-时-分"
        lightLabel.text = "-时-分"
        deepLabel.text = "-时-分"
        totalProgressView.progress = 0
        lightProgressView.progress = 0
        deepProgressView.progress = 0
    }
}
